package com.stormrage.edu.serv.student.impl;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.stormrage.constants.ErrCons;
import com.stormrage.edu.dao.student.StuAcctDao;
import com.stormrage.edu.serv.student.StuAcctServ;
import com.stormrage.edu.serv.student.StuInfoServ;
import com.stormrage.entity.adm.req.PassReq;
import com.stormrage.entity.student.StuAcct;
import com.stormrage.entity.student.StuInfo;
import com.stormrage.entity.student.req.AvatarReq;
import com.stormrage.entity.student.req.StuAcctReq;
import com.stormragetech.core.exception.StormrageTechException;
import com.stormragetech.core.util.SecureUtil;

/**
 * @Title: StuAcctServImpl.java
 * @Package com.stormrage.edu.serv.student.impl
 * @Description: 学员用户 Service
 * @author www.stormragetech.com
 * @date 2016年4月18日
 * @version V1.0
 */
@Service
public class StuAcctServImpl implements StuAcctServ {

	private static final Logger logger = LoggerFactory.getLogger(StuAcctServImpl.class);

	@Autowired
	private StuAcctDao stuAcctDao;

	@Autowired
	private StuInfoServ stuInfoServ;

	@Override
	public Integer create(StuAcctReq arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 StuAcctServ 】- 【 Create 】- 开始运行 ");

		// ------------------ 验证用户名是否被使用 ------------------
		if (arg0.getAcct().contains("stu"))
			throw StormrageTechException.instance(ErrCons.SERV_ERR, "用户名不能包含 stu 字符", StormrageTechException.ERR_APP,
					"【 StuAcctServ 】-【 Create 】");

		StuAcct acct = queryByAcct(arg0.getAcct());
		if (null != acct)
			throw StormrageTechException.instance(ErrCons.SERV_ERR, "用户名已被使用", StormrageTechException.ERR_APP,
					"【 StuAcctServ 】-【 Create 】");

		// ------------------ 添加用户 ------------------
		logger.debug(" 要添加的用户为 " + arg0.toString());
		acct = StuAcct.instance(arg0);
		logger.debug(" 要添加的用户为 " + acct.toString());
		Integer index = stuAcctDao.create(acct);

		// ------------------ 生成学号 ------------------
		acct = StuAcct.instance(acct, index);
		stuAcctDao.update(acct);

		logger.debug(" 【 Serv 】- 【 StuAcctServ 】- 【 Create 】- 完成运行 ");
		return index;
	}

	@Override
	public void update(StuAcct arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 StuAcctServ 】- 【 Update 】- 开始运行 ");

		stuAcctDao.update(arg0);

		logger.debug(" 【 Serv 】- 【 StuAcctServ 】- 【 Update 】- 完成运行 ");

	}

	@Override
	public void updateAvatar(StuAcct arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 StuAcctServ 】- 【 UpdateAvatar 】- 开始运行 ");

		stuAcctDao.updateAvatar(arg0);

		logger.debug(" 【 Serv 】- 【 StuAcctServ 】- 【 UpdateAvatar 】- 完成运行 ");

	}

	@Override
	public void updateAvatar(AvatarReq arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 StuAcctServ 】- 【 UpdateAvatar 】- 开始运行 ");

		// ------------------ 保存图片 ------------------
		String fileName = stuInfoServ.upload(arg0.getFile(), arg0.getPath());

		// ------------------ 获取用户信息 ------------------
		StuAcct acct = queryById(arg0.getAcctId());
		StuInfo info = stuInfoServ.queryByAcct(arg0.getAcctId());
		acct.setAvatar(fileName);
		info.setImage(fileName);

		// ------------------ 修改信息 ------------------
		updateAvatar(acct);
		stuInfoServ.updateImage(info);

		logger.debug(" 【 Serv 】- 【 StuAcctServ 】- 【 UpdateAvatar 】- 完成运行 ");

	}

	@Override
	public void updatePass(PassReq arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 StuAcctServ 】- 【 UpdatePass 】- 开始运行 ");

		// ------------------ 验证用户密码是否输入正确 ------------------
		StuAcct acct = queryByAcct(arg0.getAcct());
		if (null == acct) {
			throw StormrageTechException.instance(ErrCons.SERV_ERR, "用户不存在", StormrageTechException.ERR_APP,
					"【 StuAcctServImpl 】- 【 UpdatePass 】");
		}

		logger.debug(" 开始生成密码 ");
		String pass = SecureUtil.initPass(arg0.getPass(), acct.getSign());
		logger.debug(" 结束生成密码 ");
		if (!pass.equals(acct.getPass())) {
			throw StormrageTechException.instance(ErrCons.SERV_ERR, "原密码输入错误", StormrageTechException.ERR_APP,
					"【 StuAcctServImpl 】- 【 UpdatePass 】");
		}

		// ------------------ 修改用户密码 ------------------
		acct = StuAcct.instance(acct, arg0.getNpass());
		stuAcctDao.updatePass(acct);

		logger.debug(" 【 Serv 】- 【 StuAcctServ 】- 【 UpdatePass 】- 完成运行 ");

	}

	@Override
	public StuAcct queryById(Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 StuAcctServ 】- 【 QueryById 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		StuAcct entity = stuAcctDao.queryById(map);

		logger.debug(" 【 Serv 】- 【 StuAcctServ 】- 【 QueryById 】- 完成运行 ");
		return entity;

	}

	@Override
	public StuAcct queryByAcct(String arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 StuAcctServ 】- 【 QueryByAcct 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("acct", arg0);

		StuAcct entity = stuAcctDao.queryByAcct(map);

		logger.debug(" 【 Serv 】- 【 StuAcctServ 】- 【 QueryByAcct 】- 完成运行 ");
		return entity;

	}

}
